unit MLPAEditAnalysis;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, Grids, db, ADODB, DBGrids;

type
  TdlgAnalysisEdit = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    Label1: TLabel;
    cbKitID: TComboBox;
    dbgRuns: TDBGrid;
    dbgAssays: TDBGrid;
    cbRunFilter: TCheckBox;
    btnAddSelected: TButton;
    btnAddAll: TButton;
    Label2: TLabel;
    lblAssayCount: TLabel;
    procedure cbKitIDChange(Sender: TObject);
    procedure cbRunFilterClick(Sender: TObject);
    procedure btnAddSelectedClick(Sender: TObject);
    procedure btnAddAllClick(Sender: TObject);
    procedure dbgRunsTitleClick(Column: TColumn);
    procedure dbgAssaysTitleClick(Column: TColumn);
  private
    { Private declarations }
    WhereClause : String;
    procedure qryKitAssaysAfterScroll(DataSet: TDataSet);
    procedure UpdateAssayCount;
    procedure HandleDialog;
  public
    { Public declarations }
    procedure AddAnalysis;
    procedure EditAnalysis(analysisid:integer);
  end;

var
  dlgAnalysisEdit: TdlgAnalysisEdit;

implementation

uses MLPAdm, MLPAmain, MLPAOptions;

{$R *.DFM}

{ TOKBottomDlg }

procedure TdlgAnalysisEdit.AddAnalysis;
begin
  DM.GetKitIDList(cbKitId.Items);
  cbKitId.ItemIndex:=0;
  DM.tblAnalyses.InsertRecord([nil,cbKitId.Text,Now,nil,'','','','',0.8,1.25,frmMain.ColorLow,frmMain.ColorHigh, False, False]);
  HandleDialog;
end;

procedure TdlgAnalysisEdit.cbKitIDChange(Sender: TObject);
var
  probelist:TStringList;
  I: Integer;
begin
  // only allow this when analysis has no assays yet
  DM.qryKitAssays.Parameters[0].Value:=cbKitId.Text;
  DM.qryKitAssays.Requery;
  DM.tblAnalyses.Edit;
  DM.tblAnalyses.FieldValues['kitid']:=cbKitId.Text;
  // Get the default control probes
  probelist:=TStringList.Create;
  DM.GetProbeIDList(cbKitId.Text,probelist,nil);
  for I := probelist.Count - 1 downto 0 do
  begin
    if not TProbeInfo(probelist.Objects[i]).control then
    begin
      probelist.Objects[i].Free;
      probelist.Objects[i]:=nil;
      probelist.Delete(i);
    end;
  end;
  dm.tblAnalyses.FieldByName('controlprobes').AsString:=probelist.CommaText;
  DM.tblAnalyses.Post;
  for I := 0 to probeList.Count - 1 do
  begin
    probelist.Objects[i].Free;
    probelist.Objects[i]:=nil;
  end;
  probelist.Free;
end;

procedure TdlgAnalysisEdit.cbRunFilterClick(Sender: TObject);
begin
  DM.qryKitAssays.Close;
  if cbRunFilter.Checked then
  begin
    DM.qryKitAssays.SQL[2]:=WhereClause+' AND ((tbl_assays.runid) = '+IntToStr(DM.tblRunsrunid.Value)+' )'
  end
  else begin
    DM.qryKitAssays.SQL[2]:=WhereClause;
  end;
  DM.qryKitAssays.Open;
end;

procedure TdlgAnalysisEdit.dbgAssaysTitleClick(Column: TColumn);
{$J+}
 const PreviousColumnIndex : integer = -1;
{$J-}
begin
  if dbgAssays.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(dbgAssays.DataSource.DataSet) do
  begin
    if (0<=PreviousColumnIndex) and (PreviousColumnIndex<dbgAssays.Columns.Count) then
    begin
      dbgAssays.Columns[PreviousColumnIndex].title.Font.Style :=
      dbgAssays.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
    end;

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

    if (Pos(Column.Field.FieldName, string(Sort)) = 1)
    and (Pos(' DESC', string(Sort))= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

procedure TdlgAnalysisEdit.dbgRunsTitleClick(Column: TColumn);
{$J+}
 const PreviousColumnIndex : integer = -1;
{$J-}
begin
  if dbgRuns.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(dbgRuns.DataSource.DataSet) do
  begin
    if (0<=PreviousColumnIndex) and (PreviousColumnIndex<dbgRuns.Columns.Count) then
    begin
      dbgRuns.Columns[PreviousColumnIndex].title.Font.Style :=
      dbgRuns.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
    end;

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

    if (Pos(Column.Field.FieldName, string(Sort)) = 1)
    and (Pos(' DESC', string(Sort))= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

procedure TdlgAnalysisEdit.qryKitAssaysAfterScroll(DataSet: TDataSet);
begin
  cbRunFilterClick(Self);
end;

procedure TdlgAnalysisEdit.btnAddSelectedClick(Sender: TObject);
var
  analysisID:integer;
  assayid:integer;
  i:integer;
begin
  analysisID:=DM.tblAnalyses.FieldValues['analysisid'];
  for i:=0 to dbgAssays.SelectedRows.Count-1 do
  begin
    DM.qryKitAssays.GotoBookmark(pointer(dbgAssays.SelectedRows.Items[i]));
    // check for duplicates
    assayid:=DM.qryKitAssays.FieldValues['assayid'];
    if DM.AnalysisAssaysEmpty(assayid,analysisID) then
      DM.tblAnalysisAssays.InsertRecord([nil,assayid,analysisID,-1]);
  end;
  UpdateAssayCount;
end;

procedure TdlgAnalysisEdit.btnAddAllClick(Sender: TObject);
var
  analysisID:integer;
  assayid:integer;
begin
  analysisID:=DM.tblAnalyses.FieldValues['analysisid'];
  DM.qryKitAssays.First;
  While not DM.qryKitAssays.Eof do
  begin
    assayid:=DM.qryKitAssays.FieldValues['assayid'];
    if DM.AnalysisAssaysEmpty(assayid,analysisID) then
      DM.tblAnalysisAssays.InsertRecord([nil,assayid,analysisID,-1]);
    DM.qryKitAssays.Next;
  end;
  UpdateAssayCount;
end;

procedure TdlgAnalysisEdit.UpdateAssayCount;
begin
  lblAssayCount.Caption:=IntToStr(DM.getAnalysisAssayCount(DM.tblAnalyses.FieldValues['analysisid']));
  if lblAssayCount.Caption='0' then
    cbKitID.Enabled:=true
  else
    cbKitID.Enabled:=false;
end;

procedure TdlgAnalysisEdit.EditAnalysis(analysisid: integer);
begin
  DM.GetKitIDList(cbKitId.Items);
  if DM.tblAnalyses.Locate('analysisid',analysisid,[]) then
  begin
    cbKitId.ItemIndex:=cbKitId.Items.IndexOf(DM.tblAnalyses.FieldValues['kitid']);
    if cbKitId.ItemIndex>=0 then
      HandleDialog;
  end;
end;

procedure TdlgAnalysisEdit.HandleDialog;
begin
  try

    If WhereClause='' then
      WhereClause:=DM.qryKitAssays.SQL[2];
    DM.qryKitAssays.Open;
    DM.tblRuns.AfterScroll:=qryKitAssaysAfterScroll;
    cbKitIDChange(Self);
    cbRunFilter.Checked:=False;
    dbgAssays.Columns[0].Width:=64;
    UpdateAssayCount;
    ShowModal;
  finally
    DM.tblRuns.AfterScroll:=nil;
  end;
end;

end.
